<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use App\Models\Code;
use App\User;
use Illuminate\Support\Facades\Validator;

class PasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }
    
    /**
     * wangjun 
     * 增加手机短信验证
     * @param Request $req
     */
    public function postCheck(Request $req)
    {
        $data = $req->only('phone_code','verify_code','username');
        $rules = [
            'verify_code'   => 'required|captcha',
        ];
        $validate = Validator::make($data, $rules);
        if($validate->fails())
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.verify_code')])->withInput($req->only('username'));
        }
        if(!preg_match(config('app.phone_rule'),$data['username']))
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.phone_error')])->withInput($req->only('username'));
        }
        $hasUser = User::where('phone','=',$data['username'])->first();
        if(empty($hasUser))
        {
            return redirect()->back()->withErrors(['phone'=>trans('passwords.user_no')])->withInput($req->only('username'));
        }
        $nowtime = date('Y-m-d H:i:s');
        $isConti = Code::where('created_at','<',date('Y-m-d H:i:s',time()))
                    ->where('created_at','>',date('Y-m-d H:i:s',strtotime("$nowtime - 10 minute")))
                    ->where(['phone'=>$data['username'],'code'=>$data['phone_code'],'is_used'=>0,'type'=>1])->first();
        if (empty($isConti))
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.phone_code')])->withInput($req->only('username'));
        }
        return redirect('/password/phonereset')->withInput($req->only('username','phone_code'));
    }
    
    public function getPhonereset($token = null)
    {
        
        return view('auth.reset')->with('token', '')->with('reset_type',1)->with('nav',5);
    }
    
    /**
     * 通过手机重置密码
     * @param Request $req
     */
    public function postPhonereset(Request $req)
    {
        $this->validate($req, [
            'password' => 'required|confirmed|min:6',
        ]);
        
        $credentials = $req->only(
            'phone', 'password', 'password_confirmation', 'phone_code'
        );
        if(!preg_match(config('app.phone_rule'),$credentials['phone']))
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.phone_error')])->withInput($req->only('phone'));
        }
        $user = User::where(['phone'=>$credentials['phone']])->first();
        if(empty($user))
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.user_no')])->withInput($req->only('phone'));
        }
        $nowtime = date('Y-m-d H:i:s');
        $isConti = Code::where('created_at','<',date('Y-m-d H:i:s',time()))
                    ->where('created_at','>',date('Y-m-d H:i:s',strtotime("$nowtime - 10 minute")))
                    ->where(['phone'=>$credentials['phone'],'code'=>$credentials['phone_code'],'is_used'=>0,'type'=>1])->first();
        if (empty($isConti))
        {
            return redirect()->back()->withErrors(['phone'=> trans('passwords.phone_code')])->withInput($req->only('phone'));
        }
        $isConti->update(['status'=>2]);
        $this->resetPassword($user, $credentials['password']);
        
        return redirect($this->redirectPath());
    }
}
